<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('列表')"/>
    <script th:src="@{/static/admin/js/pdf.js}"></script>
    <script th:src="@{/static/admin/js/pdf.worker.js}"></script>
</head>
<style>
    .layui-tab-title {
        /*background-color: #009688;*/
        background-color: #36b368;
        color: #F5F5F5;
        border-bottom: 2px solid #CCCCCC;
    }
    .layui-this {
        background-color: #F5F5F5;
    }

    .layui-tab-item{
        background-color: #F5F5F5;
        border: 1px solid #CCCCCC;
    }

    .layui-table-box * {
        font-size: 13px !important;
        /*height:auto;*/
    }

    .layui-table-cell{
        height:auto !important;
        overflow:visible;
        text-overflow:inherit;
        white-space:normal;
        line-height: 15px !important;
    }

    .layui-table th{
        text-align: center !important;
    }
</style>
<body>
<!--<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit-->
<!--        lay-filter="monthtask-submit">-->
<!--    <i class="layui-icon layui-icon-upload-circle"></i>-->
<!--    提报-->
<!--</button>-->
<div class="layui-tab layui-tab-brief" lay-filter="test-hash">
    <ul class="layui-tab-title" id="layui-tab-title">
    </ul>
    <div class="layui-tab-content" id="layui-tab-content">
    </div>
</div>
<th:block th:include="include :: footer"/>

<!--必备条件表格&#45;&#45;自评价列单选-->
<script type="text/html" id="stateBar">
    <input type="radio" disabled="disabled" name="state{{d.id}}" value="{{d.id+'^^^'+d.typeSelf+'^^^1'}}" title="合格" {{ d.typeSelf== 1 ? 'checked' : '' }}>&nbsp;
    <input type="radio" disabled="disabled" name="state{{d.id}}" value="{{d.id+'^^^'+d.typeSelf+'^^^0'}}" title="不合格" {{ d.typeSelf== 0 ? 'checked' : '' }}>&nbsp;
</script>

<!--除”必备条件“外，其他表格”评价方式及材料“显示事件-->
<script type="text/html" id="content-event">
    {{#  if(d.type != 3){ }}
    <!--    <button class="pear-btn pear-btn-warming pear-btn-sm"-->
    <!--            lay-event="" title="填报帮助文档" onclick="viewrule('{{d.content}}')"><i class="layui-icon layui-icon-search"></i>-->
    <!--    </button>-->
    <button class="pear-btn pear-btn-primary pear-btn-sm"
            lay-event="" title="评价方式" onclick="viewrule('{{d.content}}')"><i class="layui-icon layui-icon-search"></i>
    </button>
    <button class="pear-btn pear-btn-primary pear-btn-sm"
            lay-event="" title="支撑文件查阅" onclick="supportFileView('{{d.fkParentProjectId}}')"><i class="layui-icon layui-icon-file-b"></i>
    </button>
    <button class="pear-btn pear-btn-primary pear-btn-sm"
            lay-event="" title="材料模版查阅" onclick="materialStencils('{{d.fkParentProjectId}}')"><i class="layui-icon layui-icon-template-1"></i>
    </button>
    <button class="pear-btn pear-btn-primary pear-btn-sm"
            lay-event="" title="查看填报佐证" onclick="uploadCorroboration('{{d.fkParentProjectId}}')"><i class="layui-icon layui-icon-upload"></i>
    </button>
    {{#  }else{ }}
    <span>{{d.content}}</span>
    {{#  } }}
</script>

<script th:inline="javascript" type="text/javascript">

    var tabTitleList = [[${tabTitleList}]];
    var taskId = [[${taskId}]];
    var tabTiletHtml = "";
    var tabBodyHtml = ""

    for (var i = 0; i < tabTitleList.length; i++) {
        tabTiletHtml += '<li className="layui-this" lay-id=' + tabTitleList[i].id + '>' + tabTitleList[i].content + '</li>'
        tabBodyHtml += '<div class="layui-tab-item" id="tab' + tabTitleList[i].num + '">' + '<table id="tab' + tabTitleList[i].num + '-table"></table>' + '</div>'
    }

    $('#layui-tab-title').html(tabTiletHtml);
    $('#layui-tab-content').html(tabBodyHtml);

    var MODULE_PATH = rootPath + "/MonthSubAuditController/";
    var MODULE_PATH_NEC = rootPath + "/MonthNecessaryController/";
    var MODULE_PATH_DTL = rootPath + "/MonthSupplytaskDtlController/";
    var MODULE_PATH_FILE = rootPath + "/InfoFileController/";

    layui.use(['table', 'form', 'jquery', 'element', 'laydate', 'dtree'], function () {
        //设置第一个页签默认展示
        $('#tab' + tabTitleList[0].num).attr('class', "layui-tab-item layui-show");
        $("ul li:first").attr('class', "layui-this");

        var table = layui.table;
        var form = layui.form;

        var cols0 = [
            [
                {title: '项目编号', field: 'num', align: 'center',width: 100},
                {title: '一级项目', field: 'firstProject', align: 'center',width: 150},
                {title: '二级项目', field: 'secondProject', align: 'center',width: 420},
                {title: '检查方式', field: 'testMode', align: 'center',width: 420},
                {
                    title: '评价周期', field: 'evaluationPeriod', align: 'center',width:150, templet: function (d) {
                        if (d.evaluationPeriod === '0') {
                            return '月度';
                        } else if (d.evaluationPeriod === '1') {
                            return '季度';
                        } else if (d.evaluationPeriod === '2') {
                            return '半年';
                        } else if (d.evaluationPeriod === '3') {
                            return '年初';
                        } else if (d.evaluationPeriod === '4') {
                            return '年末';
                        } else {
                            return '';
                        }
                    }
                },
                {title: '责任部门', field: 'resDeptName', align: 'center'},
                {title: '供电所自评价',field: 'typeSelf', align: 'center',templet: '#stateBar'},
            ]
        ]

        var cols1 = [
            [
                {title: '指标编号', field: 'num', align: 'center',width: 100},
                {title: '指标名称', field: 'name', align: 'center',width: 500,templet: function (d)
                    {
                        if(d.type == 3 ){
                            return '<div style="text-align:center">' + d.name + '</div>'
                        }else {
                            return '<div style="text-align:left">' + d.name + '</div>'
                        }
                    }
                },
                {
                    title: '工作要求及完成情况',
                    field: 'selfValue',
                    align: 'center',
                    width: 350,
                    templet: function (d) {
                        if (d.type === '3') {
                            return '<span >' + d.selfValue + '</span>';
                        } else {
                            if (d.tactics === '1' || d.tactics === '3') {
                                var selfValue = d.selfValue == null ? "" : d.selfValue
                                if (d.isauto == 1) {
                                    return '<input type="text" style="width: 100px;text-align:center" disabled="disabled" id = "' + d.id + '" value="' + selfValue + '"  data-old = "' + d.selfValue + '" data-num = "' + d.num + '" data-unit = "' + d.measuringUnit + '">' + '<span>' + d.measuringUnit + '</span>'
                                } else {
                                    return '<input type="text" style="width: 100px;text-align:center" disabled="disabled" id = "' + d.id + '" value="' + selfValue + '" onblur="onblurOn(this)" data-old = "' + d.selfValue + '" data-num = "' + d.num + '" data-unit = "' + d.measuringUnit + '">' + '<span>' + d.measuringUnit + '</span>'
                                }
                            } else if (d.tactics === '2') {
                                if (d.isauto == 1) {
                                    if (d.selfValue == 1) {
                                        return '<input type="radio" disabled="disabled" checked name="radio' + d.id + '" value="' + d.id + '^^^' + d.selfValue + '^^^1^^^' + d.num + '"  title="是" lay-filter="selfValueDemo">' +
                                            '<input type="radio" disabled="disabled" name="radio' + d.id + '" value="' + d.id + '^^^' + d.selfValue + '^^^0^^^' + d.num + '" title="否" lay-filter="selfValueDemo">';
                                    } else {
                                        return '<input type="radio" disabled="disabled" name="radio' + d.id + '" value="' + d.id + '^^^' + d.selfValue + '^^^1^^^' + d.num + '" title="是" lay-filter="selfValueDemo">' +
                                            '<input type="radio" disabled="disabled" checked name="radio' + d.id + '" value="' + d.id + '^^^' + d.selfValue + '^^^0^^^' + d.num + '" title="否" lay-filter="selfValueDemo">';
                                    }
                                } else {
                                    if (d.selfValue == 1) {
                                        return '<input type="radio" disabled="disabled" name="radio' + d.id + '" value="' + d.id + '^^^' + d.selfValue + '^^^1^^^' + d.num + '" title="是" lay-filter="selfValueDemo" checked>' +
                                            '<input type="radio" disabled="disabled" name="radio' + d.id + '" value="' + d.id + '^^^' + d.selfValue + '^^^0^^^' + d.num + '" title="否" lay-filter="selfValueDemo">';
                                    } else if (d.selfValue == 0) {
                                        return '<input type="radio" disabled="disabled" name="radio' + d.id + '" value="' + d.id + '^^^' + d.selfValue + '^^^1^^^' + d.num + '" title="是" lay-filter="selfValueDemo" >' +
                                            '<input type="radio" disabled="disabled" name="radio' + d.id + '" value="' + d.id + '^^^' + d.selfValue + '^^^0^^^' + d.num + '" title="否" lay-filter="selfValueDemo" checked>';
                                    } else {
                                        return '<input type="radio" disabled="disabled" name="radio' + d.id + '" value="' + d.id + '^^^' + d.selfValue + '^^^1^^^' + d.num + '" title="是" lay-filter="selfValueDemo" >' +
                                            '<input type="radio" disabled="disabled" name="radio' + d.id + '" value="' + d.id + '^^^' + d.selfValue + '^^^0^^^' + d.num + '" title="否" lay-filter="selfValueDemo">';
                                    }
                                }
                            } else {
                                return '';
                            }
                        }
                    }
                },
                {
                    title: '评价周期', field: 'evaluationPeriod', align: 'center', templet: function (d) {
                        if (d.evaluationPeriod === '0') {
                            return '月度';
                        } else if (d.evaluationPeriod === '1') {
                            return '季度';
                        } else if (d.evaluationPeriod === '2') {
                            return '半年';
                        } else if (d.evaluationPeriod === '3') {
                            return '年初';
                        } else if (d.evaluationPeriod === '4') {
                            return '年末';
                        } else {
                            return '';
                        }
                    }
                },
                {
                    title: '评价方式及材料',
                    field: 'fkParentProjectId',
                    align: 'center',
                    width: 300,
                    templet: '#content-event',
                    merge: true
                },
                {title: '项目分值', field: 'score', align: 'center',merge: true},
                {title: '供电所自评分', field: 'proscore', align: 'center',merge: true}
                // {title: '操作', toolbar: '#infoProject-bar', align: 'center'}
            ]
        ]

        //table表单列表
        table.render({
            elem: '#tab0-table',
            url: MODULE_PATH_NEC + 'list2/' + [[${taskId}]],
            page: false,
            method: 'get',
            cols: cols0,
            skin: 'line',
            id: 'tab0-table', scrollPos: 'fixed', loading: false,
            done: function (res) {
                res.data.forEach(function (item, index) {
                    if (item.type == 3) {
                        var tr = $(".layui-table").find("tbody tr[data-index='" + index + "']");
                        tr.css("background-color", "rgb( 0 , 176 , 80 )");
                        tr.find(".layui-table-cell").css("color", "#ffffff");
                    }
                });
            }
        });
        var table_data = new Array();
        for (var j = 1; j < tabTitleList.length; j++) {
            //table表单列表
            table.render({
                elem: '#tab' + j + '-table',
                url: MODULE_PATH_DTL + 'list4subaudit/' + [[${taskId}]] + '/' + j,
                page: false,
                method: 'get',
                cols: cols1,
                skin: 'line',
                id: 'tab' + j + '-table', scrollPos: 'fixed',
                loading: false,
                done: function (res) {
                    var that = this.elem.next();
                    table_data = res.data;
                    for (var i = 0; i < res.data.length; i++) {
                        if (res.data[i].type == 3) {
                            var index = res.data[i]['LAY_TABLE_INDEX'];
                            var tr = that.find(".layui-table tr[data-index='" + index + "']");
                            // tr.css("cssText", "height:100px !important");
                            tr.css("background", "rgba(192,252,179,0.35)");
                            tr.find("a[lay-event='put']").prop('disabled', true);
                            tr.find("a[lay-event='put']").removeClass("layui-btn-normal").addClass("layui-btn-disabled");
                        }
                    }
                    merge(this);
                }

            });
        }
        //支撑文件-d为id
        window.supportFileView = function (d) {
            console.log(d)
            var index = layer.open({
                type: 2,
                title: '在线查看支撑文件',
                shade: 0.1,
                area: ['1300px', '650px'],
                maxmin: true,
                // content: MODULE_PATH_FILE + 'upload/' + d +'/1'
                content: MODULE_PATH_FILE + 'fileview/' + d + '/1/' + taskId
            });
            // layer.full(index)
        }
        //材料模版 -d为id
        window.materialStencils = function (d) {
            //下载连接
            // window.open('/restaurantRes/basicManagement/2023-6/df15602c-fed6-44d9-b0a7-78e0eb58ff3d/2.pdf')
            var index = layer.open({
                type: 2,
                title: '模板材料查看',
                shade: 0.1,
                area: ['1300px', '650px'],
                content: MODULE_PATH_FILE + 'templatedownload/' + d + '/2/' + taskId
            });
        }

        //查看佐证 -d为id
        window.uploadCorroboration = function (d) {
            var index = layer.open({
                type: 2,
                title: '查看填报佐证',
                shade: 0.1,
                area: ['1300px', '650px'],
                maxmin: true,
                content: MODULE_PATH_FILE + 'fileview/' + d + '/3/' + taskId
            });
            // layer.full(index)
        }

        //查看评分细则
        window.viewrule = function (d) {
            layer.open({
                type: 1,
                title: '评价方式',
                area: ['500px', '500px'],
                // content: '<div><textarea name="description">'+d+'</textarea></div>'
                content: d
            });
        }

        //提报按钮绑定
        form.on('submit(monthtask-submit)', function (data) {
            layer.confirm('审核结果提交后不可修改，是否确认提交？', {
                title: "提交确认",
                icon: 0,
                btn: ['确认', '取消'] //可以无限个按钮
            }, function(data){
                $.ajax({
                    url: MODULE_PATH + '/subauditCommit',
                    data: JSON.stringify({"id": taskId}),
                    dataType: 'json',
                    contentType: 'application/json',
                    type: 'post',
                    success: function (result) {
                        if (result.code == 200) {
                            layer.msg("审核提报完成", {icon: 1, time: 3000}, function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.location.reload(); //刷新父页面
                                //parent.layer.close(index);
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 3000});
                        }
                    }
                })
            }, function(data){
                location.reload();
            });
        });
    })


    function merge(myTable, skipFlag = true) {
        var tableBox = $(myTable.elem).next().children('.layui-table-box'),
            $main = $(tableBox.children('.layui-table-body').children('table').children('tbody').children('tr').toArray().reverse()),
            $fixLeft = $(tableBox.children('.layui-table-fixed-l').children('.layui-table-body').children('table').children('tbody').children('tr').toArray().reverse()),
            $fixRight = $(tableBox.children('.layui-table-fixed-r').children('.layui-table-body').children('table').children('tbody').children('tr').toArray().reverse()),
            cols = myTable.cols[0], mergeRecord = {};

        for (let i = 0; i < cols.length; i++) {
            var item3 = cols[i], field = item3.field;
            if (item3.merge) {
                var mergeField = [field];
                if (item3.merge !== true) {
                    if (typeof item3.merge == 'string') {
                        mergeField = [item3.merge]
                    } else {
                        mergeField = item3.merge
                    }
                }
                mergeRecord[i] = {mergeField: mergeField, rowspan: 1}
            }
        }

        $main.each(function (i) {
            for (var _recordIndex in mergeRecord) {
                var _mergeRecord = mergeRecord[_recordIndex];
                //检查前一个字段
                var _beforeMergeRecord = mergeRecord[_recordIndex - 1];
                //
                if (i === $main.length - 1 || (skipFlag && _beforeMergeRecord && _beforeMergeRecord.rowspan === 1) || isMaster(i, _mergeRecord)) {
                    $(this).children('[data-key$="-' + _recordIndex + '"]').attr('rowspan', _mergeRecord.rowspan);
                    $fixLeft.eq(i).children('[data-key$="-' + _recordIndex + '"]').attr('rowspan', _mergeRecord.rowspan);
                    $fixRight.eq(i).children('[data-key$="-' + _recordIndex + '"]').attr('rowspan', _mergeRecord.rowspan);
                    _mergeRecord.rowspan = 1;
                } else {
                    $(this).children('[data-key$="-' + _recordIndex + '"]').remove();
                    $fixLeft.eq(i).children('[data-key$="-' + _recordIndex + '"]').remove();
                    $fixRight.eq(i).children('[data-key$="-' + _recordIndex + '"]').remove();
                    _mergeRecord.rowspan += 1;
                }
            }
        })

        function isMaster(index, _mergeRecord) {
            var mergeField = _mergeRecord.mergeField;
            var dataLength = layui.table.cache[myTable.id].length;
            for (var i = 0; i < mergeField.length; i++) {

                if (layui.table.cache[myTable.id][dataLength - 2 - index][mergeField[i]]
                    !== layui.table.cache[myTable.id][dataLength - 1 - index][mergeField[i]]) {
                    return true;
                }
            }
            return false;
        }
    }
</script>
</body>
</html>